//
//  EmoticonViewModel.swift
//  阳光表情键盘
//
//  Created by 阿拉斯加的狗 on 15/12/15.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import Foundation


class EmoticonManager {

    //创建单例
    static let sharedManager = EmoticonManager()
    
    
    //表情包模型
    lazy var packages = [EmoticonPackage]()
    
    init(){
        
        //添加分组
        packages.append(EmoticonPackage(dict: ["group_name_cn": "最近A"]))
    
        //加载文件
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        
        //加载字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        
        // 获取id 的数组
        let array = (dict["packages"] as! NSArray).valueForKey("id")
        
        //遍历id数组 准备加载info.plist
        for id in array as! [String] {
        
            loadInfoPlist(id)
        
        }
    }
    
    
    //加载每一个id目录下得info.plist
    private func loadInfoPlist(id: String) {
    
        //1.创建路径
        let path = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle/\(id)")!
        
        //2.加载字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        
        //3.字典转模型  追加到 packages 数组中
        packages.append(EmoticonPackage(dict: dict))
    
    
    }
}